var appDirective = angular.module("appDirective",[]);

appDirective.directive("appHead",function(appDirectiveService,$location){
	return {
		templateUrl: "../enroll/directive/template/app.head.directive.html?rev=201512190230",
		restrice: "ACEM",
		replace: true,
		transclude: true,
		scope:{
			login:"="
		},
		controller:function($scope,$element,$attrs){

			$(window).bind("scroll",function(){
				if($(this).scrollTop()>100){
					$(".start_top").show();
				}
				if($(this).scrollTop()<100){
					$(".start_top").hide();
				}
			})

			$scope.top = function(){
				$("html,body").animate({"scrollTop":"0px"},500);
			}

			if(appDirectiveService.isPc()){
				$scope.showWx = true;
				$scope.showList = false;
			}else{
				$scope.showWx = false;
				$scope.showList = false;
			}

			if($scope.login == true){
				$scope.isLogin = true;
				$scope.pic = urlPic + appDirectiveService.getLocalStorage("start-pic");
				$scope.name = appDirectiveService.getLocalStorage("start-name");
			}else{
				$scope.isLogin = false;
			}

			var watch = $scope.$watch("login",function(newValue,oldValue){
				if(newValue == true){
					$scope.isLogin = true;
					$scope.pic = urlPic + appDirectiveService.getLocalStorage("start-pic");
				}else if(newValue == false){
					$scope.isLogin = false;
				}
			})

			$scope.showPop = function(){
				$scope.pop = true;
			}

			$scope.hidePop = function(){
				$scope.pop = false;
			}

			$scope.triggleOptionsList = function(){
				$scope.showList = !$scope.showList;
			}

			$scope.login = function(){
				$location.path("/user/login");
			}

			$scope.userCenter = function(){
				if(appDirectiveService.userIsLogin()){
					$location.path("/user/mindex")
				}else{
					alert("您还未登录！");
					$location.path("/user/login")
				}
			}

			$scope.logout = function(){
				var logoutConfirm = confirm("您确定要退出吗？");
				if(logoutConfirm){
					localStorage.removeItem("start-uuid");
					localStorage.removeItem("start-name");
					localStorage.removeItem("start-pic");
					alert("退出成功！");
					$location.path("/user/login");
					$scope.isLogin = false;
				}
			}
		}
	}
})

appDirective.service("appDirectiveService",function(){
	this.isPc = function(){
	    var userAgentInfo = navigator.userAgent;  
	    var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
	    var flag = true;    
	    for (var v = 0; v < Agents.length; v++) {    
	        if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
	    }    
	    return flag;      
	}

	this.getLocalStorage = function(key){
		var value = JSON.parse(localStorage.getItem(key)).value;
		return value;
	}

	this.userIsLogin = function(){
		var uuidStr = localStorage.getItem("start-uuid");

		if( uuidStr == null || "undefined" == typeof uuidStr){
			return false;
		}else{
			var uuid = JSON.parse(uuidStr);
			var now = parseInt(new Date().valueOf());

			if( (now-parseInt(uuid.createtime)) >= parseInt(uuid.expire) ){
				localStorage.removeItem("start-uuid");
				localStorage.removeItem("start-name");
				localStorage.removeItem("start-pic");
				return false;
			}else{
				return true;
			}
		}
	}
})